package com.tuanzi.loan.business.repository.businesssystem.impl;

import com.tuanzi.loan.business.repository.businesssystem.custom.BusinessPrepayRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.businesssystem.BusinessPrepayRequest;
import com.tuanzi.loan.vo.response.businesssystem.BusinessPrepayResponse;
import org.jooq.Condition;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.Date;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanBusinessChannelMain.LOAN_BUSINESS_CHANNEL_MAIN;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanBusinessPrepay.LOAN_BUSINESS_PREPAY;


public class BusinessPrepayRepositoryImpl extends JOOQBaseRepositoryPlus implements BusinessPrepayRepositoryCustom {
    @Override
    protected TableImpl getTable() {
        return LOAN_BUSINESS_PREPAY;
    }

    @Override
    public Page<BusinessPrepayResponse> list(BusinessPrepayRequest searchRequest, Pageable pageable) {
        String companyName = searchRequest.getCompanyName();
        Integer mainType = searchRequest.getMainType();
        Integer payType = searchRequest.getPayType();
        Integer prepayType = searchRequest.getPrepayType();
        Integer billStatus = searchRequest.getBillStatus();
        String payAccount = searchRequest.getPayAccount();
        String updator = searchRequest.getUpdator();
        Date startTime = searchRequest.getStartTime();
        Date endTime = searchRequest.getEndTime();
        fields(LOAN_BUSINESS_PREPAY.fields());
        fields(LOAN_BUSINESS_CHANNEL_MAIN.CHANNEL_MAIN,LOAN_BUSINESS_CHANNEL_MAIN.BANK_ID,LOAN_BUSINESS_CHANNEL_MAIN.MAIN_TYPE);
        leftJoin(LOAN_BUSINESS_CHANNEL_MAIN,LOAN_BUSINESS_CHANNEL_MAIN.ID.eq(LOAN_BUSINESS_PREPAY.COMPANY_ID));
        if (companyName!=null)
        eq(LOAN_BUSINESS_CHANNEL_MAIN.CHANNEL_MAIN,companyName);
        eq(LOAN_BUSINESS_CHANNEL_MAIN.MAIN_TYPE,mainType);
        eq(LOAN_BUSINESS_PREPAY.PAY_MODE,payType);
        eq(LOAN_BUSINESS_PREPAY.PAY_TYPE,prepayType);
        if(billStatus!=null &&billStatus==1){
            eq(LOAN_BUSINESS_PREPAY.PREPAY_STATUS,billStatus);
        }else if (billStatus!=null && billStatus==0){
            isNull(LOAN_BUSINESS_PREPAY.PREPAY_STATUS);
        }
        eq(LOAN_BUSINESS_PREPAY.PREPAY_AMOUNT,payAccount);
        eq(LOAN_BUSINESS_PREPAY.UPDATOR,updator);
        between(LOAN_BUSINESS_PREPAY.PREPAY_TIME,startTime,endTime);
        orderBy(LOAN_BUSINESS_PREPAY.PREPAY_TIME, Sort.Direction.DESC);

        Page<BusinessPrepayResponse> search = search(pageable, record -> {
            BusinessPrepayResponse response = new BusinessPrepayResponse();
            response.setBillStatus(record.get(LOAN_BUSINESS_PREPAY.PREPAY_STATUS,Integer.class));
            response.setId(record.get(LOAN_BUSINESS_PREPAY.ID));
            response.setCompanyName(record.get(LOAN_BUSINESS_CHANNEL_MAIN.CHANNEL_MAIN));
            response.setMainType(record.get(LOAN_BUSINESS_CHANNEL_MAIN.MAIN_TYPE)==null?0:record.get(LOAN_BUSINESS_CHANNEL_MAIN.MAIN_TYPE).intValue());
            response.setPrepayTime(record.get(LOAN_BUSINESS_PREPAY.PREPAY_TIME));
            response.setPrepayAmount(record.get(LOAN_BUSINESS_PREPAY.PREPAY_AMOUNT));
            response.setPayType(record.get(LOAN_BUSINESS_PREPAY.PAY_MODE));
            response.setPrepayType(record.get(LOAN_BUSINESS_PREPAY.PAY_TYPE,Integer.class));
            response.setPayProof(record.get(LOAN_BUSINESS_PREPAY.PAY_PROOF));
            response.setBankId(record.get(LOAN_BUSINESS_CHANNEL_MAIN.BANK_ID));
            response.setRemark(record.get(LOAN_BUSINESS_PREPAY.REMARK));
            response.setUpdator(record.get(LOAN_BUSINESS_PREPAY.UPDATOR));
            response.setUpdateTime(record.get(LOAN_BUSINESS_PREPAY.UPDATE_TIME));
            return response;
        });
        return search;
    }

    @Override
    public Double getAllPrepayIncome(BusinessPrepayRequest $) {
        String companyName = $.getCompanyName();
        Date startTime = $.getStartTime();
        Date endTime = $.getEndTime();
        Condition condition = LOAN_BUSINESS_PREPAY.ID.isNotNull();
        if (companyName!=null&&companyName!="")
            condition= condition.and(LOAN_BUSINESS_CHANNEL_MAIN.CHANNEL_MAIN.eq(companyName));
        if (startTime!=null&&endTime!=null)
            condition=  condition.and(LOAN_BUSINESS_PREPAY.PREPAY_TIME.ge(new java.sql.Date(startTime.getTime()))).and(LOAN_BUSINESS_PREPAY.PREPAY_TIME.le(new java.sql.Date(endTime.getTime())));
        Double into = dslContext.select(DSL.sum(LOAN_BUSINESS_PREPAY.PREPAY_AMOUNT).filterWhere(LOAN_BUSINESS_PREPAY.PAY_TYPE.ne((byte)4)).subtract(DSL.sum(LOAN_BUSINESS_PREPAY.PREPAY_AMOUNT).filterWhere(LOAN_BUSINESS_PREPAY.PAY_TYPE.eq((byte)4)))).from(LOAN_BUSINESS_PREPAY).leftJoin(LOAN_BUSINESS_CHANNEL_MAIN)
                .on(LOAN_BUSINESS_CHANNEL_MAIN.ID.eq(LOAN_BUSINESS_PREPAY.COMPANY_ID.cast(Integer.class)))
                .where(condition).fetchOne().into(Double.class);
        return into;
    }
}
